<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2010-12-11
 * Time: 13:36:08
 * To change this template use File | Settings | File Templates.
 */
 
class newsComponents extends sfComponents
{
    function getDataRows($sqlStr)
    {
        $dbBase = new DbBase();
        return $dbBase->getDataQuery($sqlStr);
    }

    function executeNews()
    {
        if($this->recordCount == null)
            $this->recordCount = 10;
        $query = Doctrine_Core::getTable("News")->createQuery();
        if($this->catalogId != null)
            $query->where("catalog_id=?",$this->catalogId);
        if($this->orderByCreateTime != null)
            $query->orderBy("create_time desc");
        else
        {
            if($this->orderbyHit)
                $query->orderBy("hit_i desc");
            else
                $query->orderBy("create_time desc");
        }
        //$this->newss = $news->execute();
        $this->html = "数据库中暂无记录。";
        $news = $query->execute();
        if($news->count() > 0)
        {
            $this->html = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
            foreach($news as $currentNews)
            {
                $this->html = $this->html."<tr>";
                $this->html = $this->html."<td width=\"5%\">选择<input type=\"checkbox\" name=\"".$currentNews->getNewsId()."\" value=\"".$currentNews->getCatalogId()."\" id=\"".$currentNews->getNewsId()."\" onClick='onRelatedCheckClick(this,\"news\")' /></td>";
                $this->html = $this->html."<td  width=\"76%\">".$currentNews->getTitle()."</td>";
                $this->html = $this->html."<td  width=\"19%\">".date("y年m月d日 H:i:s",$currentNews->getCreateTime())."</td>";
                $this->html = $this->html."</tr>";
            }
            $this->html = $this->html."</table>";
        }
    }

    function executeGetSearchByTag()
    {
        if ($this->searchType == null)
            $this->searchType = "news";
        $isEdit = false;
        if($this->isEdit != null)
            $isEdit = true;
        $tableName = "";
        $this->html = "没找到相关记录。";
        if($this->recordCount == null)
            $this->recordCount  = 10;
        //$this->html = $this->whereStr;
        switch($this->searchType)
        {
            case "news":
                $tableName="news";
                $sqlStr = "select news_id as id, title,(case when hit_i is null then 0 else hit_i end) as hit_i,(case when hit_mask_i is null then 0 else hit_mask_i end) as hit_mask_i, create_time,catalog_id, publish_time from news";
                break;
            case "photo":
                $tableName ="photo";
                $sqlStr = "select  photo_group_id as id, title,(case when hit_i is null then 0 else hit_i end) as hit_i,(case when hit_mask_i is null then 0 else hit_mask_i end) as hit_mask_i, create_time,catalog_id, publish_time from PhotoGroup";
                break;
            case "video":
                $tableName ="video";
                $sqlStr = "select  video_id as id, title, (case when hit_i is null then 0 else hit_i end) as hit_i,(case when hit_mask_i is null then 0 else hit_mask_i end) as hit_mask_i, create_time,catalog_id, publish_time from Video";
                break;
        }

        if($this->whereStr != null)
        {}
        if($tableName != "")
        {
            $whereStr = "";
            //$currentWhereStr = "";
            if($this->whereStr != null)
            {

                $this->whereStr = @str_replace(";",",",$this->whereStr);
//                $this->html = $this->whereStr;
//                return ($this->whereStr);
//                if($isEdit == true)
//                {
//                    $relatedArray = explode(":",$this->whereStr);
//                    foreach($relatedArray as $where => $value)
//                    {
//                         $whereStr = $value;
//                         break;
//                    }
//                }
//                else
                    $whereStr = $this->whereStr;
                $sqlStr = $sqlStr." where ".$whereStr;
                //$currentWhereStr = $currentWhereStr.$whereStr;
            }
            //$sqlStr = $sqlStr." and status_id=1005" ;
            if($this->orderStr != null)
                $sqlStr = $sqlStr." order by ".$this->orderStr;
            else
                $sqlStr = $sqlStr." order by publish_time desc limit ".$this->recordCount;

            $data_rows = $this->getDataRows($sqlStr);
            //$this->html = $sqlStr;//count($data_rows);
            $currentRecordCount = 0;
            if(count($data_rows) > 0)
            {
                $this->html = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
                $this->html = $this->html.$this->getSearchTrHtml($data_rows,$tableName,null);

                if($isEdit == true && count($data_rows) < $this->recordCount)
                {
//                     $relatedArray = explode(":",$this->whereStr);
//                     foreach($relatedArray as $where =>$value)
//                     {
//                         if($value != $whereStr)
//                         {
//                             if(trim($value) != "")
//                             {
//                                 $tempWhere = $this->getWhereStr($value);
//                                 $sqlStr = str_replace("where ".$whereStr,"where ".$tempWhere." and tag !='".$value."' ",$sqlStr);
//                                 $data_rows = $this->getDataRows($sqlStr);
//                                 $this->html = $this->html.$this->getSearchTrHtml($data_rows,$tableName,$whereStr);
//                             }
//                             break;
//                         }
//                     }
                    if($this->tag != null && $this->notInWhereStr != null)
                    {
                        $this->notInWhereStr = @str_replace(";",",",$this->notInWhereStr);
                        $tempWhere = $this->notInWhereStr." and ".$this->getWhereStr($this->tag).' and status_id != 1006 ';
                        $sqlStr = str_replace("where ".$whereStr,"where ".$tempWhere,$sqlStr);
                        $data_rows = $this->getDataRows($sqlStr);
                        $this->html = $this->html.$this->getSearchTrHtml($data_rows,$tableName,$whereStr);
                    }
                }
                $this->html = $this->html."</table>";
            }
        }
    }

    function getSearchTrHtml($data_rows,$tableName,$relatedIdStr)
    {
        $html = "";
        foreach($data_rows as $currentRow)
        {
             //if($relatedIdStr == null || strrpos($relatedIdStr,$currentRow["id"],0) == false)
             {
                 $html = $html."<tr>";
                 $html = $html."<td width=\"8%\">选择<input type=\"checkbox\" name=\"".$currentRow["id"]."\" value=\"".$currentRow["catalog_id"]."\" id=\"".$tableName.$currentRow["id"]."\" onClick='onRelatedCheckClick(this,\"".$tableName."\")' /></td>";
                 $html = $html."<td width=\"60%\">".$currentRow["title"]."</td>";
                 $html = $html."<td width=\"10%\">".($currentRow["hit_mask_i"] + $currentRow["hit_i"])."</td>";
                 $html = $html."<td align=\"right\" width=\"22%\">".date("Y年m月d日 H:i:s",strtotime($currentRow["create_time"]))."</td>";
                 $html = $html."</tr>";
             }
        }
       return $html;
    }


    function getWhereStr($sourceStr)
    {
        $tagArray = preg_split("/，|,/",$sourceStr);
        $whereStr = "";
        foreach($tagArray as $where => $value)
        {
            if($whereStr == "")
                $whereStr = $whereStr."(tag like '%".$value."%' ";
            else
                $whereStr = $whereStr." or tag like '%".$value."%' ";
        }
        $whereStr = $whereStr.") ";
        return $whereStr;
    }

}
